﻿using iBuproModel.Providers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using iBuproModel;
using System.Linq;

namespace IBuproModel.Tests
{
    
    
    /// <summary>
    ///This is a test class for IBuproRoleProviderTest and is intended
    ///to contain all IBuproRoleProviderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IBuproRoleProviderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for IBuproRoleProvider Constructor
        ///</summary>
        [TestMethod()]
        public void IBuproRoleProviderConstructorTest()
        {
            IBuproRoleProvider target = new IBuproRoleProvider();
            Assert.AreNotEqual(null, target);
        }

        /// <summary>
        ///A test for AddUsersToRoles
        ///</summary>
        ///
        [ExpectedException(typeof(NotImplementedException))]
        [TestMethod()]
        public void AddUsersToRolesTest()
        {
            IBuproRoleProvider target = new IBuproRoleProvider();
            target.AddUsersToRoles(null, null);
        }

        /// <summary>
        ///A test for CreateRole
        ///</summary>
        [TestMethod()]
        public void CreateRoleTest()
        {
            string roleName = "aaa";
            IBuproRoleProvider rp = new IBuproRoleProvider();
            if (rp.RoleExists(roleName))
                rp.DeleteRole(roleName,false);

            rp.CreateRole(roleName);
            Assert.AreEqual(rp.RoleExists(roleName), true);
            rp.DeleteRole(roleName,false);

        }

        /// <summary>
        ///A test for DeleteRole
        ///</summary>
        [TestMethod()]
        public void DeleteRoleTest()
        {
            string roleName = "aaa";
            IBuproRoleProvider rp = new IBuproRoleProvider();

            if (!rp.RoleExists(roleName))
                rp.CreateRole(roleName);

            rp.DeleteRole(roleName,false);
            Assert.AreEqual(rp.RoleExists(roleName),false);
                
        }

        /// <summary>
        ///A test for FindUsersInRole
        ///</summary>
        ///
        [ExpectedException(typeof(NotImplementedException))]
        [TestMethod()]
        public void FindUsersInRoleTest()
        {
            IBuproRoleProvider target = new IBuproRoleProvider();
            target.FindUsersInRole(null, null);
        }

        /// <summary>
        ///A test for GetAllRoles
        ///</summary>
        [TestMethod()]
        public void GetAllRolesTest()
        {
            IBuproRoleProvider rp = new IBuproRoleProvider();
            string[] roleNames = rp.GetAllRoles();
            Assert.IsNotNull(roleNames);

            
        }

        /// <summary>
        ///A test for GetRolesForUser
        ///</summary>
        ///
        [ExpectedException(typeof(Exception),"Username cannot be null")]
        [TestMethod()]
        public void GetRolesForUserTest()
        { 
                IBuproRoleProvider rp = new IBuproRoleProvider();
                rp.GetRolesForUser(null);      
        }

        /// <summary>
        ///A test for GetUsersInRole
        ///</summary>
        /// 
        [ExpectedException(typeof(NotImplementedException))]
        [TestMethod()]
        public void GetUsersInRoleTest()
        {
            IBuproRoleProvider rp = new IBuproRoleProvider();
            rp.GetUsersInRole(null);
        }

        /// <summary>
        ///A test for IsUserInRole
        ///</summary>
        [TestMethod()]
        public void IsUserInRoleTest()
        {
            string roleName = "aaa";
            string userName = "myusername";
            IBuproRoleProvider rp = new IBuproRoleProvider();

            

           

            if (!rp.RoleExists(roleName))
                rp.CreateRole(roleName);


            Entities context = new Entities();



            User user = new User();
            user.FirstName = "aaa";
            user.LastName = "bbb";
            user.Email = "eee@www.cz";
            user.Roles.Add(context.Roles.Where(r => r.Name == roleName).SingleOrDefault());
            user.Username = userName;
            user.Salt = "aaa";
            user.Password = "zxcxzcvxcvxczv";

            context.Users.AddObject(user);

            context.SaveChanges();

            Assert.AreEqual(rp.IsUserInRole(userName, roleName),true);

            

            context.Users.DeleteObject(user);
            rp.DeleteRole(roleName,false);


        }

        /// <summary>
        ///A test for RemoveUsersFromRoles
        ///</summary>
        [ExpectedException(typeof(NotImplementedException))]
        [TestMethod()]
        public void RemoveUsersFromRolesTest()
        {
            IBuproRoleProvider rp = new IBuproRoleProvider();
            rp.RemoveUsersFromRoles(null, null);
        }

        /// <summary>
        ///A test for RoleExists
        ///</summary>
        [TestMethod()]
        public void RoleExistsTest()
        {
            string roleName = "myrole4654";
            IBuproRoleProvider rp = new IBuproRoleProvider();
            rp.CreateRole(roleName);

            Assert.AreEqual(rp.RoleExists(roleName), true);
            rp.DeleteRole(roleName, false);
        }

        /// <summary>
        ///A test for ApplicationName
        ///</summary>
        [ExpectedException(typeof(NotImplementedException), "Not implemented")]
        [TestMethod()]
        public void ApplicationNameTest()
        {
            IBuproRoleProvider rp = new IBuproRoleProvider();
            string appName = "ibupro";

            Assert.AreEqual(appName, rp.ApplicationName); ;

            rp.ApplicationName = "something";
            
        }

    }
}
